;
(function(global) {
    var task, img, styleTask, styleImg;

    function createLoading() {
        task = document.createElement('div');
        task.style.cssText = 'position:fixed;background:rgba(0,0,0,0);left:0;top:0;width:100%;height:100%;z-index:9999';
        document.documentElement.appendChild(task);
    };

    function removeLoading() {
        document.documentElement.removeChild(task);
        task = null;
    };

    function JSBridge() {
        this.name = 'JSBridge';
        this.reset = true;
    };

    JSBridge.prototype.device = function() {
        var u = navigator.userAgent;
        var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1;
        var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/);
        return isAndroid;
    };
    /**
     * @param null
     * JSBridge初始化
     */
    JSBridge.prototype._init = function(callback) {
        if (window.WebViewJavascriptBridge) {
            return callback(WebViewJavascriptBridge);
        } else {
            document.addEventListener('WebViewJavascriptBridgeReady', function(WebViewJavascriptBridge) {
                return callback(WebViewJavascriptBridge);
            }, false);
        }
        if (window.WVJBCallbacks) { return window.WVJBCallbacks.push(callback); }
        window.WVJBCallbacks = [callback];
        var WVJBIframe = document.createElement('iframe');
        WVJBIframe.style.display = 'none';
        WVJBIframe.src = 'wvjbscheme://__BRIDGE_LOADED__';
        document.documentElement.appendChild(WVJBIframe);
        setTimeout(function() { document.documentElement.removeChild(WVJBIframe) }, 0)
    };
    /**
     * @param null
     * JSBridge 安卓初始化回调接口
     */
    JSBridge.prototype.__init__ = function(bridge) {
        var isAn = this.device();
        if (this.reset && isAn) {
            this.reset = false;
            bridge.init(function(message, responseCallback) {
                console.log('JS got a message', message);
                var data = {
                    'Javascript Responds': '测试中文!'
                };
                console.log('JS responding with', data);
                responseCallback(data);
            });
        }
    };


    /*
     * 调用原生方法
     */
    JSBridge.prototype.callHandler = function(funcName, data, callback) {
        this._init(function(bridge) {
            this.__init__(bridge);
            bridge.callHandler(funcName, data, function(response) {
                if(callback)
                    callback(JSON.parse(response));
            })
        }.bind(this))
    };

    /*
    * H5注册方法
    */
    JSBridge.prototype.registerHandler = function(handlerName, callback) {
        var isAn = this.device();
        //处理android WebViewJavascriptBridge延时问题
        if(isAn){
            document.addEventListener('WebViewJavascriptBridgeReady', function() {
                JSBridge.prototype._init(function(bridge) {
                    JSBridge.prototype.__init__(bridge);
                    bridge.registerHandler(handlerName, function(resData,responseCallback) {
                        callback();
                        responseCallback("访问成功");
                    })
                }.bind(this))
            }, false);
        }else{
            JSBridge.prototype._init(function(bridge) {
                JSBridge.prototype.__init__(bridge);
                bridge.registerHandler(handlerName, function(resData,responseCallback) {
                    callback();
                    responseCallback("访问成功");
                })
            }.bind(this));
        }
    };
    
    /*
     * 全局变量设置
     */
    if (typeof module !== 'undefined' && module.exports) {
        module.exports = JSBridge;
    } else if (typeof define === 'function' && (define.amd || define.cmd)) {
        define(function() {
            return JSBridge;
        });
    } else {
        global.JSBridge = JSBridge;
    };
})(this);